#import "FlutterPlugin_aSendMsgToFluuter.h"

@interface FlutterPlugin_aSendMsgToFluuter()
@property (nonatomic, strong) FlutterBasicMessageChannel *channel;
@property (atomic, assign) BOOL isRegister;

+ (FlutterPlugin_aSendMsgToFluuter *)sharedInstance;
@end


@implementation FlutterPlugin_aSendMsgToFluuter

+ (FlutterPlugin_aSendMsgToFluuter *)sharedInstance {
    static FlutterPlugin_aSendMsgToFluuter *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[FlutterPlugin_aSendMsgToFluuter alloc] init];
    });
    
    return instance;
}

+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
    FlutterBasicMessageChannel *channel = [FlutterBasicMessageChannel messageChannelWithName:@"send_event_msg_2_flutter_channel" binaryMessenger:[registrar messenger]];
    
    FlutterPlugin_aSendMsgToFluuter *instance = [self sharedInstance];
    instance.channel = channel;
    
    [instance performSelector:@selector(test) withObject:nil afterDelay:1.0f];
}

- (void)test {
    long start = [self getNow];
    
    if (@available(iOS 10.0, *)) {
        [NSTimer scheduledTimerWithTimeInterval:5 repeats:YES block:^(NSTimer * _Nonnull timer) {
            long run = [self getNow] - start;
            [self.channel sendMessage:[NSNumber numberWithLong:run]];
            
            if (run > 100) {
                [timer invalidate];
            }
        }];
    } else {
        // Fallback on earlier versions
    }
}

- (long)getNow {
    NSDate *date = [NSDate date];
    long timeStamp = [date timeIntervalSince1970];
    return timeStamp;
}

@end
